<template>
  <el-dialog
    :title="!dataForm.orgId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="120px">
      <el-form-item label="上级机构ID" prop="orgParentId">
        <el-input v-model="dataForm.orgParentId" placeholder="上级机构ID"></el-input>
      </el-form-item>
      <el-form-item label="上级机构名称" prop="orgParentName">
        <el-input v-model="dataForm.orgParentName" placeholder="上级机构名称"></el-input>
      </el-form-item>
      <el-form-item label="机构名称" prop="orgName">
        <el-input v-model="dataForm.orgName" placeholder="机构名称"></el-input>
      </el-form-item>
      <el-form-item label="机构层级" prop="orgDepth">
        <el-input v-model="dataForm.orgDepth" placeholder="机构层级"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          orgId: 0,
          orgName: '',
          orgDepth: 1,
          orgParentId: 1,
          orgParentName: '阳光康养院'
        },
        dataRule: {
          orgName: [
            { required: true, message: '机构名称不能为空', trigger: 'blur' }
          ],
          orgDepth: [{ required: true, message: '机构层级不能为空', trigger: 'blur' }]
        }
      }
    },
    methods: {
      init (orgId) {
        this.dataForm.orgId = orgId || 0
        
        if (this.dataForm.orgId) {
          this.$http({
            url: this.$http.adornUrl(`/sys/org/info/${this.dataForm.orgId}`),
            method: 'get',
            params: this.$http.adornParams()
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.dataForm.orgName = data.org.orgName;
              this.dataForm.orgParentId = data.org.orgParentId;
              this.dataForm.orgParentName = data.org.orgParentName;
              this.visible = true;
            }
          })
        }else{
          debugger;
            this.visible = true;
            this.$nextTick(() => {
              this.$refs['dataForm'].resetFields()
            })
        }
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/sys/org/${!this.dataForm.orgId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'orgId': this.dataForm.orgId || undefined,
                'orgName': this.dataForm.orgName,
                'orgDepth': this.dataForm.orgDepth,
                'orgParentId': this.dataForm.orgParentId
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
